package model;

import java.sql.ResultSet;
import java.util.ArrayList;
import database.EstateOperations;

public class EstateCollection 
{
	private ArrayList<Estate> estlist;
	private ResultSet rst;
	private EstateOperations eo;
	public EstateCollection(EstateOperations e)
	{
		this.eo=e;
		estlist=new ArrayList<Estate>();
	}
	
	
	
	public Estate getEstate(int i) 
	{
		return estlist.get(i);
	}

	public int getNumEstates() 
	{
		return estlist.size();
	}
	
	public boolean removeEstate(int id) 
	{
		boolean ans=false;
		for (int i = 0; i < estlist.size(); i++) 
		{
			if (id==estlist.get(i).getId()) 
			{
				estlist.remove(i);
				eo.deleteEstate(id);
				ans= true;
			}
		}
		return ans;
	}
	
	public void addEstate()
	{
		rst = eo.getLastRow();
		try 
		{
			Estate e = new Estate(rst.getInt(1), rst.getString(2),rst.getDouble(3));
			estlist.add(e);
			
		} 
		catch (Exception e) 
		{
			System.out.println(e);
		}
	}
	public void updatePrice(int id,double price) 
	{
		for (int i = 0; i < estlist.size(); i++) 
		{
			if (id==estlist.get(i).getId())
			{
				estlist.get(i).setPrice(price);;
		
			}
		}	
		//eo.updateEstatePrice(id,price);
	}
	
	
	public int findEstate(int id) 
	{
		int index = -1;
		for (int i = 0; i < estlist.size(); i++) 
		{
			if (id==estlist.get(i).getId()) 
			{
				index = i;
			}
		}
		return index;
	}
}
